<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/6
 * Time: 14:49
 */

namespace app\common\traits;
use app\admin\model\State;
use app\admin\controller\Auth;
use app\admin\model\AuthRule;
use think\Db;
use think\facade\Cache;

trait Gate
{
    public static $auth; //服务层对象
    private static $uid; //服务层对象
    private static $authrule; //服务层对象
    //静态方法，单例统一访问入口
    static public function getAuth() {
        if (is_null ( self::$auth ) || isset ( self::$auth )) {
            self::$auth     = new Auth();
        }
        return self::$auth;
    }
    //静态方法，单例统一访问入口
    static public function getAuthRules() {
        if (is_null ( self::$authrule ) || isset ( self::$authrule )) {
            self::$authrule     = new AuthRule();
        }
        return self::$authrule;
    }
    //静态方法，单例统一访问入口
    static public function getUid() {
        if (is_null ( self::$uid ) || isset ( self::$uid )) {
            self::$uid     =  is_login();
        }
        return self::$uid;
    }
    /**
     * 判断该用户有该角色
     * @param $role 用户标识
     * @param bool $uid
     * @return bool
     */
    public function hasRole($role, $uid = '')
    {
        if(empty($role))return false;
        if (empty($uid))$uid = self::getUid();
        $group = self::getAuth()->getUserGroup($uid);
        $access = [];
        foreach ($group as $v){
            $access[] = $v['mark'];
        }
        if(is_array($role)){
            $temp = array_intersect($role,$access);
            if(!empty($temp))return true;
        }else{
            if(in_array($role, $access)){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断用户是否有某一权限 例如 admin/index/index,edit_art.
     * @param string $rule
     * @return bool
     */
    public function checkRule($rule='')
    {
        return self::getAuthRules()->check_auth($rule);
    }

    /**
     * 根据传入的状态id 判断用户操作状态的权限
     * @param null $state
     * @return array|bool
     */
    public function checkState($state = null)
    {
        $arr = Cache::tag('state')->get('state'.self::getUid());
        $arr?:$arr = $this->getGroupIds();
        if(!is_null($state)){
            if(!State::where(['id'=>$state,'status'=>1])->find()) return false;
            return in_array($state,array_keys($arr));
        }
        return $arr;
    }

    /**
     * 根据用户的id获取用户的角色的已经授权的id
     * @return array
     */
    public function getGroupIds()
    {
        $usergroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $stateids = Db::name('status_group')->where(['auth_id'=>['in',$usergroupids]])->column('status_id');
        $statusids = Db::name('state')->where(['status'=>2])->column('id');
        $arr = array_diff(array_unique($stateids),$statusids);//排除
        $data = Db::name('state')->where(['id'=>['in',$arr]])->column('name','id');
        Cache::tag('state')->set('state'.self::getUid(),$data);
        return $data;
    }

    /**
     * 获取用户状态id
     * @param string $uid
     * @return array
     */
    public function getUserGroupStatus($uid = ''){
        if (empty($uid))$uid = self::getUid();
        $group = self::getAuth()->getUserGroup($uid);
        $access = '';
        foreach ($group as $v){
            $access+= $v['hidstates'].',';
        }
        return explode(',',$access);
    }

    public function getCanEditStates(){
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $caneditstates = Db::name('auth_group')->where(['id'=>['in',$authgroupids]])->field('caneditstates')->select();
        return strToArr($caneditstates,'caneditstates');
    }
    public function getEditStates(){
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $editstates = Db::name('auth_group')->where(['id'=>['in',$authgroupids]])->field('editstates')->select();
        return strToArr($editstates,'editstates');
    }
    public function getHidStates(){
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $hidstates = Db::name('auth_group')->where(['id'=>['in',$authgroupids]])->field('hidstates')->select();
        return strToArr($hidstates,'hidstates');
    }
    public function getBaomiqx(){
        $authgroupids = self::getAuth()->getUserGroupids(self::getUid());//获取用户id为$id的角色权限组的id
        $baomi = Db::name('auth_group')->where(['id'=>['in',$authgroupids]])->field('baomi')->cache(6000)->find();
        return $baomi['baomi'];
    }


    /**获取个人用户需要保密的状态
     * @return mixed
     */
//    public function getUserBaomiStates(){
//        return Db::name('users')->where(['id'=>session('admin_user.adminid')])->column('baomi_states');
//    }

    /**获取个人用户负责的医院
     * @return mixed
     */
    public function getUserToYiyuan(){
        $info = Db::name('users')->where(['id'=>session('admin_user.adminid')])->field('own_yiyuan')->find();
        return $info['own_yiyuan'];
    }

}